<template>
  <section class="form-section">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="手机号码:" prop="telephone">
        <el-input placeholder="请输入手机号" v-model="form.telephone" />
      </el-form-item>
      <el-form-item label="短信验证码:" prop="authCode">
        <div class="code-box">
          <el-input placeholder="请输入短信验证码" v-model="form.authCode" />
          <CCCountDown @click="onCodeClick" ref="codebtn" />
        </div>
      </el-form-item>
      <div style="text-align:center">
        <el-button class="login-btn" type="primary" @click="onSubmit">下一步</el-button>
      </div>
    </el-form>
  </section>
</template>

<script >
  import CCCountDown from '@/components/common/CCCountDown.vue';
  import { doSmsCode } from '@/apis/user';
  import ZZToast from '@/utils/toast';

  export default {
    name: 'ForgetStep1',
    components: { CCCountDown },
    data() {
      return {
        form: {
          telephone: '',
          authCode: ''
        },
        rules: {
          telephone: [{
            trigger: 'blur',
            validator: (rule, value, callBack) => {
              if (!value) callBack('请输入手机号');
              if (/^[1][0-9]{10}$/.test(value)) callBack();
              else callBack('手机号码不合法');
            }
          }],
          /*
          telephone: [
            { required: true, message: '请输入手机号', trigger: 'blur' }, {
              trigger: 'blur',
              validator: (rule, value, callBack) => {
                if (/^[1][0-9]{10}$/.test(value)) callBack();
                else callBack('手机号码不合法');
              }
            }
          ],*/
          authCode: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
        }
      };
    },

    methods: {
      async onCodeClick() {
        console.log('into click!!');
        this.rules.telephone[0].validator(null, this.form.telephone, async(msg) => {
          if (msg) return;
          const res = await doSmsCode({ telephone: this.form.telephone });
          if (res.code !== 200) {
            ZZToast.warn(res.message);
          } else {
            this.$refs.codebtn.run();
            this.cache.saveSmsTime();
          }
        });
      },

      async onSubmit() {
        const valid = await this.$refs.form.validate();
        if (!valid) return;
        const data = { ...this.form };
        /*
        const check = await doCheckAuthcode(data);
        if ((check == null) || (check.code !== 200)) {
          return;
        } else {
        */
        this.$emit('next', data);
      }
    },

    mounted() {
      let t = this.cache.fetchSmsTime();
      if (!t) return;
      t = Math.floor((new Date().getTime() - t) / 1000);
      if (t > 0 && t < 60) this.$refs.codebtn.run(60 - t);
    }
  };
</script>

<style lang="scss" type="text/scss" scoped>
.form-section {
  display: flex;
  justify-content: center;
  .el-form {
    width: 400px;
    color: white;
    background: white;
    .el-input {
      width: 100%;
    }
    .login-btn {
      width: 130px;
      margin-top: 12px;
    }
  }
}

.code-box {
  @include v-center();
  .el-input {
    margin-right: 12px;
  }
}
</style>
